# coding=utf-8
"""
Created on 2018年08月21日
"""
import boto3


class AwsS3Service(object):
    """
    亚马逊cdn服务
    """

    def __init__(self):
        """
        """
        self.bucket_name = 'oregon-service'

    def resource_s3(self):
        """
        获取s3对象
        """
        return boto3.resource('s3')

    def if_file_exist(self, file_name):
        """
        检查文件是否存在
        注意: file_name 为bucket_name后的路径, 例如 'test/profile/2018/08/22/test.jpeg'
        """
        s3 = self.resource_s3()
        bucket = s3.Bucket(self.bucket_name)
        for obj in bucket.objects.filter(Prefix=file_name):
            if obj.key == file_name:
                return True
        return False


if __name__ == '__main__':
    s3_service = AwsS3Service()
    r = s3_service.if_file_exist(file_name='test/profile/2018/08/22/test.jpeg')
    print(str(r))

